<?php
/*
+--------------------------------------------------------------------------
|   AffiliStore 2
|   ========================================
|   Web: http://www.affilistore.com
|   Email: admin (at) affilistore (dot) com
|	License Type: AffiliStore 2 is NOT open source software and limitations apply 
|   Licence Info: Visit AffiliStore website and click on 'Licence'
+--------------------------------------------------------------------------
*/
// checks to see if password session is set
// if not redirects to ../admin/
if($_SESSION['loggedIn'] == 0 && !isset($adminPage)){
include('../includes/auth.inc.php');
}


if (isset($_POST['dd_updated'])) {
$sql = sprintf("UPDATE affiliSt_config SET value = %s WHERE name = 'dbHost'",
		quote_smart($_POST['dd_host']));
mysql_query($sql, $databaseConnect) or die(mysql_error());
$sql = sprintf("UPDATE affiliSt_config SET value = %s WHERE name = 'dbUsername'",
		quote_smart($_POST['dd_user']));
mysql_query($sql, $databaseConnect) or die(mysql_error());
$sql = sprintf("UPDATE affiliSt_config SET value = %s WHERE name = 'dbPassword'",
		quote_smart($_POST['dd_pass']));
mysql_query($sql, $databaseConnect) or die(mysql_error());
$sql = sprintf("UPDATE affiliSt_config SET value = %s WHERE name = 'dbName'",
		quote_smart($_POST['dd_db']));
mysql_query($sql, $databaseConnect) or die(mysql_error());



// write values posted to connect.php include file
$file = fopen("../includes/connect.php", "w");

$cData ='<?php'."\r\n";
$cData .='$hostname = "'.$dd_host.'";'."\r\n";
$cData .='$database = "'.$dd_db.'";'."\r\n";
$cData .='$username = "'.$dd_user.'";'."\r\n";
$cData .='$password = "'.$dd_pass.'";'."\r\n";
$cData .='$databaseConnect = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);'."\r\n";
$cData .='?>';

fwrite($file, $cData);
fclose($file);

}

$getUserDetails = mysql_query("SELECT * FROM affiliSt_config");
$userDetails = mysql_fetch_assoc($getUserDetails);

?>
<fieldset>
<legend>Update Your Database Details</legend>
<form action="index.php?ad=dd" method="post" name="updateDetails">
<?php
if (isset($_POST['dd_updated'])) {
echo '<strong style="color:green">Database Details Updated</strong><br />';
}
?>
<table cellpadding="4" cellspacing="0" border="0">
<?php do { ?>
<?php if ($userDetails['name'] == 'dbHost') { ?>
<tr>
<td>
<label for="MySQLHost">MySQL Host:</label>
</td>
<td><input name="dd_host" type="text" id="MySQLHost" class="adminInput" value="<?php echo $userDetails['value'];?>" />
</td>
</tr>
<?php } ?>
<?php if ($userDetails['name'] == 'dbUsername') { ?>
<tr>
<td>
<label for="MySQLUsername">MySQL Username:</label>
</td>
<td>
<input name="dd_user" type="text" id="MySQLUsername" class="adminInput" value="<?php echo $userDetails['value'];?>" />
</td>
</tr>
<?php } ?>
<?php if ($userDetails['name'] == 'dbPassword') { ?>
<tr>
<td>
<label for="MySQLPassword">MySQL Password:</label>
</td>
<td>
<input name="dd_pass" type="password" id="MySQLPassword" class="adminInput" value="<?php echo $userDetails['value'];?>" />
</td>
</tr>
<?php } ?>
<?php if ($userDetails['name'] == 'dbName') { ?>
<tr>
<td>
<label for="MySQLDb">MySQL Database Name:</label>
</td>
<td>
<input name="dd_db" type="text" id="MySQLDb" class="adminInput" value="<?php echo $userDetails['value'];?>" />
</td>
</tr>
<?php } ?>
<?php } while ($userDetails = mysql_fetch_assoc($getUserDetails)); ?>
</table>
<input type="hidden" name="dd_updated" value="yes" />
<input value="Update Your Details" type="submit" class="padSubmit" />
</form>
</fieldset>